package day03;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * @author Xiao
 * @date 2021/8/19
 */
public class JDBCDemo03 {
    /**
     * JDBC API详解：
     * 	JDBC 事务管理：Connection接口中定义了3个对应的方法
     * a.开启事务：setAutoCommit(boolean autoCommit)——true为自动提交事务；false为手动提交事务，即为开启事务。
     * b.提交事务：commit()
     * c.回滚事务：rollback()
     */
    public static void main(String[] args) throws Exception {
        String url = "jdbc:mysql:///day03?useSSL=false";
        String username = "root";   // 数据库用户名
        String password = "1234";   // 数据库密码
        Connection conn = DriverManager.getConnection
                (url, username, password);  // 获取连接
        Statement stmt = conn.createStatement();
        String sql = "select * from account";
        ResultSet rs = stmt.executeQuery(sql);
        /*while (rs.next()) {
            int id = rs.getInt(1);
            String name = rs.getString(2);
            double money = rs.getDouble(3);
            System.out.println(id + "\t" + name + "\t" + money);
            System.out.println("-----------------");
        }*/
        System.out.println("======================");
        while (rs.next()) {
            int id = rs.getInt("id");
            String name = rs.getString("name");
            double money = rs.getDouble("money");
            System.out.println(id + "\t" + name + "\t" + money);
            System.out.println("-----------------");
        }
        rs.close();
        stmt.close();
        conn.close();
    }
}
